//
//  Bead.m
//  Abacus
//
//  Created by user on 11. 1. 30..
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "Bead.h"
#import "Abacus.h"

@implementation Bead

@synthesize orgX;
@synthesize orgY;
@synthesize maxX;
@synthesize minX;
@synthesize posX;
@synthesize uniqueId;

- (id)initWithFrame:(CGRect)frame {
    if ((self = [super initWithFrame:frame])) {
        // Initialization code
    }
    return self;
}

//===============================================================
// touchesBegan
//===============================================================
- (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event {
	NSLog(@"=========================");
	CGPoint pt = [[touches anyObject] locationInView:self];
	startLocation = pt;
	[[self superview] bringSubviewToFront:self];
}

//===============================================================
// touchesMoved
//===============================================================
- (void) touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event {
	CGPoint pt = [[touches anyObject] locationInView:self];
	CGRect frame = [self frame];
	
	frame.origin.x += pt.x - startLocation.x;
	if (frame.origin.x > maxX) {
		frame.origin.x = maxX;
	} else if (frame.origin.x < minX) {
		frame.origin.x = minX;
	}

	[self setFrame:frame];
}

//===============================================================
// Touch Move
//===============================================================
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
	//CGPoint pt = [[touches anyObject] locationInView:self];
	CGRect frame = [self frame];

	// 반이상 이동시 완전이동으로 처리 또는 원위치
	if (frame.origin.x > (minX + 20 / 2)) {
		frame.origin.x = maxX;
	} else {
		frame.origin.x = minX;
	}
	[self setFrame:frame];
}

//===============================================================
// Touch 취소
//===============================================================
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
	// 진행하기 전단계로 모두 이동
}


- (void)setDefault:(int) seq x:(int) x y:(int) y {
	uniqueId = seq;
	orgX = x;
	orgY = y;
	// 5자리수
	if (seq % 5 == 0) {
		maxX = x;
		minX = x - MAX_MOVE;
	}
	// 1자리수
	else {
		minX = x;
		maxX = x + MAX_MOVE;	
	}
}

- (void)dealloc {
    [super dealloc];
}


@end
